package com.example.yblin.hello24.DataUtils;


import android.content.Context;
import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by LJL on 2018/04/24.
 */

public class RanklistDataUtil {

    private List<RankItemData> rankItemDataList;
    private Map<String, String> datalist;

    public final static int MODE_STANDARD = 0;
    public final static int MODE_STAGE = 1;

    public int getMode() {
        return mode;
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    private int mode = MODE_STANDARD;




    public RanklistDataUtil() {
        rankItemDataList = new ArrayList<>();

    }

    public List<RankItemData> getRankItemDataList() {
        return rankItemDataList;
    }

    public int achieveFromInternet(Context context) {

        Map<String, String> loginInfo = new HashMap<>();
        loginInfo.put("username", LocalDataUtil.read(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.USERNAME));
        loginInfo.put("pwd", LocalDataUtil.read(context, LocalDataUtil.DATABASE_USERDATA, LocalDataUtil.PASSWORD));

        //loginInfo.put("username", "新垣结衣");
        //loginInfo.put("pwd", "1");

        NetUtil netUtil = null;
        if (mode == MODE_STANDARD) {
            netUtil = new NetUtil(NetUtil.ADDR_RANKLIST, loginInfo);
        } else {
            netUtil = new NetUtil(NetUtil.ADDR_STAGE_RANKLIST, loginInfo);
        }
        Map<String, String> returnData = null;

        returnData = netUtil.getData();

        if (returnData.get("code").equals("" + NetUtil.CODE_OK)) {
            rankItemDataList.clear();
            for (Map.Entry<String, String> entry :
                    returnData.entrySet()) {
                if (!entry.getKey().equals("code")) {
                    Map<String, String> unit = JsonUtil.analyze(entry.getValue());
                    Boolean error = true;
                    RankItemData data = new RankItemData();

                    if (mode == MODE_STANDARD) {
                        data.setRank(Integer.parseInt(unit.get("rank")));
                        data.setUsername(unit.get("username"));
                        data.setRightNum(Integer.parseInt(unit.get("rightnum")));
                        data.setTotalNum(Integer.parseInt(unit.get("totalnum")));
                        data.setRate(unit.get("rate"));
                        data.setCostTime(Integer.parseInt(unit.get("costtime")));
                        rankItemDataList.add(data);
                    } else {
                        data.setRank(Integer.parseInt(unit.get("rank")));
                        data.setUsername(unit.get("username"));
                        data.setStage(Integer.parseInt(unit.get("stage")));
                        rankItemDataList.add(data);
                    }


                }

            }
        }

        return Integer.parseInt(returnData.get("code"));

    }

    public class RankItemData {
        private int rank;
        private String username;
        private int rightNum;
        private int totalNum;
        private String rate;
        private int costTime;
        private int stage;

        public int getStage() {
            return stage;
        }

        public void setStage(int stage) {
            this.stage = stage;
        }

        public RankItemData() {

        }

        public RankItemData(int rank, String username, int rightNum, int totalNum, String rate, int costTime) {
            this.rank = rank;
            this.username = username;
            this.rightNum = rightNum;
            this.totalNum = totalNum;
            this.rate = rate;
            this.costTime = costTime;
        }

        public int getRank() {
            return rank;
        }

        public void setRank(int rank) {
            this.rank = rank;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public int getRightNum() {
            return rightNum;
        }

        public void setRightNum(int rightNum) {
            this.rightNum = rightNum;
        }

        public int getTotalNum() {
            return totalNum;
        }

        public void setTotalNum(int totalNum) {
            this.totalNum = totalNum;
        }

        public String getRate() {
            return rate;
        }

        public void setRate(String rate) {
            this.rate = rate;
        }

        public int getCostTime() {
            return costTime;
        }

        public void setCostTime(int costTime) {
            this.costTime = costTime;
        }

    }
}
